class _Time {
    constructor({ duration = 1000, everyDuration = undefined, dateSeparator = '-', timeSeparator = ':', final = null, times = 60, date = new Date() } = {}) {
        this.isRuning = false;
        this._$date = ''; //保存初次传入的时间
        this.stamp = 0; //时间戳
        this.hours = 0;
        this.times = 60; //timeout时的次数
        this.minutes = 0;
        this.seconds = 0;
        this.year = 1990;
        this.month = 0;
        this.day = 0;
        this.duration = 1000;
        this._dateSeparator = '-'; //存储内部分隔符
        this._timeSeparator = ':'; //存储内部分隔符
        this.date = '';
        this.time = '';
        this.ap = '';
        // 主程序
        this.interval = () => {
            let self = this;
            if (this.isRuning) {
                this._time = this._date = this._$date = new Date();
                this.everyDuration && (this.everyDuration(this));
                this._id = setTimeout(() => {
                    self.interval();
                }, self.duration);
            }
            else {
                clearTimeout(this._id);
            }
        };
        this.init(date);
        this.times = times;
        this.finally = final;
        this.duration = duration;
        this.everyDuration = everyDuration;
        this.dateSeparator = dateSeparator;
        this.timeSeparator = timeSeparator;
    }
    init(date) {
        this.isRuning = false;
        this._time = this._date = this._$date = date; //初始化时间和日期
        this.stamp = this._date.getTime();
    }
    // 日期分割符
    get dateSeparator() {
        return this._dateSeparator;
    }
    set dateSeparator(separator) {
        this._dateSeparator = separator;
        this._date = this._$date;
    }
    // 时间分隔符
    get timeSeparator() {
        return this._timeSeparator;
    }
    set timeSeparator(separator) {
        // this._timeSeparator = separator
        this._time = this._$date;
    }
    // 日期
    get _date() {
        return this._$date;
    }
    set _date(date) {
        this.year = date.getFullYear();
        this.month = date.getMonth() + 1;
        this.day = date.getDate();
        this.date = '' + this.year + this.dateSeparator + this.formNumber(this.month) + this.dateSeparator + this.formNumber(this.day);
    }
    // 时间
    get _time() {
        return this._$date.getTime();
    }
    // 更新时间
    set _time(date) {
        this.hours = date.getHours();
        this.minutes = date.getMinutes();
        this.seconds = date.getSeconds();
        this.ap = this.hours <= 12 ? 'AM' : 'PM';
        this.time = '' + this.formNumber(this.hours) + this.timeSeparator + this.formNumber(this.minutes) + this.timeSeparator + this.formNumber(this.seconds);
    }
    formNumber(num) {
        return num < 10 ? '0' + num : '' + num;
    }
    // 开始计时
    start() {
        this.isRuning = true;
        this.interval();
    }
    // 暂停
    stop() {
        this.isRuning = false;
        clearTimeout(this._id);
        console.log(`time is up.`);
    }
    // 静态方法
    static formatT(date) {
        return (new Date(Date.parse(date.split(' ').join('T')))).getTime();
    }
    get weekMap() {
        return new Map([
            [0, '日'],
            [1, '一'],
            [2, '二'],
            [3, '三'],
            [4, '四'],
            [5, '五'],
            [6, '六']
        ]);
    }
    week(type) {
        if (!type) {
            return this._date.getDay();
        }
        else {
            return this.weekMap.get(this._date.getDay());
        }
    }
    // 倒计时函数功能
    timeout() {
        let self = this;
        let cd = this.everyDuration;
        this.everyDuration = function () {
            if (self.times > 0) {
                self.times -= 1;
                cd && (cd(self));
            }
            else {
                self.stop();
                self.finally && (self.finally(self));
            }
        };
        this.start();
    }
}
export default _Time;
